﻿using System.Web.Mvc;
using YogaStudio.Service;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    public class ShoppingCartController : Controller
    {
        private readonly IProductService _productService;
        private readonly IShoppingCartService _shoppingCartService;

        public ShoppingCartController() : this(new ProductService(), new ShoppingCartService()) { }

        public ShoppingCartController(IProductService productService, IShoppingCartService shoppingCartService)
        {
            _productService = productService;
            _shoppingCartService = shoppingCartService;
        }

        public ActionResult Index()
        {
            _shoppingCartService.SetCartId(HttpContext);
            var cart = _shoppingCartService.GetCartItems();

            ViewBag.TotalCartItems = _shoppingCartService.GetCount();
            ViewBag.TotalCartPrice = _shoppingCartService.GetTotal();

            return View(cart);
        }

        [HttpPost]
        public ActionResult AddToCart(int id)
        {
            //var productToAdd = _productService.GetProductyId(id);
            _shoppingCartService.SetCartId(HttpContext);
            _shoppingCartService.AddToCart(id);
            TempData["Message"] = "Item added to cart successfully";
            return RedirectToAction("ProductDetails", "Product", new {id});
        }

        [HttpPost]
        public ActionResult RemoveItemFromCart(int id)
        {
            _shoppingCartService.RemoveFromCart(id);
            TempData["Message"] = "1 Item Removed Successfully";
            return RedirectToAction("Index");
        }

        public ActionResult CartSummary()
        {
            _shoppingCartService.SetCartId(HttpContext);

            ViewBag.CartCount = _shoppingCartService.GetCount();
            ViewBag.CartTotal = _shoppingCartService.GetTotal();

            return PartialView("CartSummary");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _shoppingCartService.Dispose(true);
                _productService.Dispose(true);
            }
            base.Dispose(disposing);
        }
    }
}